$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var machine = localStorage.getItem("gjxrollMachine");
    var work = "";
    var controlvalue_D = 2.8; // T
    var controlvalue_E = 0.5; // T
    var pass = 1;
    var focus = true;
    var qualityJson = {
        zj: "",
        bmzl: ""
    };
    var apiUrl = "scheduleplan";

    $(".head .icon-lishi1").on("click", function () {
        var machine = $("section span.machine").html();
        if (machine != "空") {
            localStorage.setItem("gjxrollMachine", machine);
        }
        window.location.href = "gjxroll_lishi.html";
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    $("div.page").on("click", "span.personal", function () {
        localStorage.removeItem("gjxrollMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    if (machine) {
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/gjxroll/getGjxrollMaterialByMachine',
            data: {
                machine: machine
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    // var data = res.result;
                    // $alert(val + "扫描成功");
                    //填充 当前机台与当前用料
                    showplandata(res);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    }

    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描层卷机台！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxroll/getGjxrollMaterialByMachine',
                    data: {
                        machine: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            $alert(val + "扫描成功");
                            //填充 当前机台与当前用料
                            showplandata(res);
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    function showplandata(res) {
        var data = res.result;
        //填充 当前机台与当前用料
        $("section span.machine").html(data.machine.machineCode);
        $("section span.machineName").html(data.machine.machine);
        if (data.steelStrandTwist && data.steelStrandTwist.length > 0) {
            var ngBatchHtml = "<option value=''>请选择捻股产品</option>";
            for (var i = 0; i < data.steelStrandTwist.length; i++) {
                ngBatchHtml += "<option value='" + data.steelStrandTwist[i].batch + "'>" + data.steelStrandTwist[i].batch + "</option>"
            }
            $("section select.ngBatch").html(ngBatchHtml);
            // 默认 选择 最后一个
            if (data.steelStrandTwist && data.steelStrandTwist.length > 0) {
                $("section select.ngBatch").val(data.steelStrandTwist[data.steelStrandTwist.length - 1].batch).trigger("change");
            }
        }
        $("section span.work_name").html("空");
        $("section span.work_dd").html("空");
        $("section span.work_zl").html("空");
        $("section span.work_mc").html("空");
        $("section span.work_zucd").html("空");
        $("section span.work_zusycd").html("空");
        $("section span.work_cjCount").html("空");
        $("section span.work_cjLength").html("空");
    }
    //组号 选择的时候
    $("section select.ngBatch").on("change", function () {
        var batch = $(this).val();
        getSteelStrandTwistData(batch);
    });
    function getSteelStrandTwistData(batch) {
        if (!batch) {
            $("section span.work_name").html("空");
            $("section span.work_dd").html("空");
            $("section span.work_zl").html("空");
            $("section span.work_mc").html("空");
            $("section span.work_zucd").html("空");
            $("section span.work_zusycd").html("空");
            $("section span.work_cjCount").html("空");
            $("section span.work_cjLength").html("空");
            return;
        }
        loading_show("获取中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "POST",
            url: url + "/app/gjxroll/getSteelStrandTwistData",
            data: {
                machine: $("section span.machine").html(),
                batch: batch
            },
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    loading_hide();
                    var data = res.result;
                    $alert("获取成功！");
                    $("section span.work_name").html(data.inventoryName);
                    $("section span.work_dd").html(data.breakpoint || "");
                    $("section span.work_zl").html(data.itemWeight);
                    $("section span.work_mc").html(data.itemLength);
                    $("section span.work_zucd").html(data.availableLength);
                    $("section span.work_zusycd").html(data.syLength);
                    $("section span.work_cjCount").html(data.cjCount || 0);
                    $("section span.work_cjLength").html(data.cjLength || 0);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    }

    //点击头检验
    $(".bnt .check_tou").click(function () {
        var machine = $("section span.machine").html();
        if (!machine || machine == "空") {
            $alert("请先扫描层卷机台");
        }
        var ngBatch = $("section select.ngBatch").val();
        if (!ngBatch) {
            return $alert("请先选择组号！");
        }
        $.confirm("确定头检验吗？", function () {
            if (pass == 0) return;
            pass = 0;
            loading_show("提交中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxroll/checkHeat',
                data: {
                    batch: $("section select.ngBatch").val(),
                    machine: $("section span.machine").html()
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        $alert("头检验成功！");
                        setTimeout(function () {
                            getSteelStrandTwistData($("section select.ngBatch").val());
                        }, 1200);
                        // 打印 层卷 标签
                        if (res.result) {
                            takeupPrint("gjxrollTakeup", res.result);
                        }
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击尾检验
    $(".bnt .check_wei").click(function () {
        var machine = $("section span.machine").html();
        if (!machine || machine == "空") {
            $alert("请先扫描层卷机台");
        }
        var ngBatch = $("section select.ngBatch").val();
        if (!ngBatch) {
            return $alert("请先选择组号！");
        }
        $.confirm("确定尾检验吗？", function () {
            if (pass == 0) return;
            pass = 0;
            loading_show("提交中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxroll/checkFoot',
                data: {
                    batch: $("section select.ngBatch").val(),
                    machine: $("section span.machine").html()
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        $alert("尾检验成功！");
                        setTimeout(function () {
                            getSteelStrandTwistData($("section select.ngBatch").val());
                        }, 1200);
                        // 打印 层卷 标签
                        if (res.result) {
                            takeupPrint("gjxrollTakeup", res.result);
                        }
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击 收线
    $(".bnt .shouxiao").click(function () {
        var machine = $("section span.machine").html();
        if (!machine || machine == "空") {
            return $alert("请先扫描层卷机台号");
        }
        //
        if ($("section select.ngBatch").val()) {
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxroll/getDataByMachine',
                dataType: 'json',
                data: {
                    type: "收线",
                    machine: $("section span.machine").html()
                },
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        loading_hide();
                        focus = false;
                        $(".mengceng").show();
                        $("div.div_shouxian").show();
                        $("div.div_shouxian input").val("");
                        $("div.div_shouxian input.actualDiameter").focus();
                        $("div.div_shouxian input.length").val(data ? data.length : "");
                        $("div.div_shouxian select").val("合格").trigger("change");
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //隐藏提示符 显示
                    loading_hide();

                    if (error.status == 0) {
                        $alert("请求超时，请再试一次！");
                    } else {
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        } else {
            $alert("请先选择组号！");
        }
    });
    //点击 收线 取消
    $("div.div_shouxian .sx_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_shouxian").hide();
        $("#goods_id").focus();
        focus = true;
    });

    // 实测直径 work.inventoryDiameter
    /*$("div.div_shouxian input.actualDiameter").on('change, blur', function () {
        if ($("div.div_shouxian input.actualDiameter").val()) {
            // 超出±0.03
            if (Number(accSub($("div.div_shouxian input.actualDiameter").val(), work.inventoryDiameter)) > 0) {
                $alert("直径有差，请确认", 1000);
            } else if (Number(accSub($("div.div_shouxian input.actualDiameter").val(), work.inventoryDiameter)) < 0) {
                $alert("直径有差，请确认", 1000);
            }
        }
    })*/

    //点击 收线 确定
    $(".sx_queding").click(function () {
        if (pass == 0) {
            return;
        }
        pass = 0;
        if (!$("div.div_shouxian input.actualDiameter").val()) {
            pass = 1;
            $alert("实测直径不可为空");
            $("div.div_shouxian input.actualDiameter").focus();
        }
        else if ($("div.div_shouxian input.actualDiameter").val() < 0) {
            pass = 1;
            $alert("实测直径不可小于0");
            $("div.div_shouxian input.actualDiameter").focus();
        }
        else if (!$("div.div_shouxian input.length").val()) {
            pass = 1;
            $alert("机台米长不可为空");
            $("div.div_shouxian input.length").focus();
        }
        else if (Number($("div.div_shouxian input.length").val()) <= 0) {
            pass = 1;
            $alert("机台米长不可小于0");
            $("div.div_shouxian input.length").focus();
        }
        else {
            if ($("div.div_shouxian .bmzl").val() == "不合格") {
                qualityJson.bmzl = "表面质量不合格";
            } else {
                qualityJson.bmzl = "";
            }
            var bhgx = [];
            for (var k in qualityJson) {
                if (qualityJson[k]) {
                    bhgx.push(qualityJson[k]);
                }
            }
            //调用 收线 接口
            loading_show("收线中...");
            //层卷 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxroll/gjxrollTakeup',
                data: {
                    batch: $("section select.ngBatch").val(),
                    machine: $("section span.machine").html(),
                    length: $("div.div_shouxian input.length").val(),
                    actualDiameter: $("div.div_shouxian input.actualDiameter").val(),
                    bmzl: $("div.div_shouxian select.bmzl").val(),
                    selfResult: $("div.div_shouxian select.selfResult").val(),
                    bhgx: JSON.stringify(bhgx),
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        $alert("收线成功！");
                        setTimeout(function () {
                            $(".mengceng").hide();
                            $("div.div_shouxian").hide();
                            $("#goods_id").focus();
                            focus = true;
                            getSteelStrandTwistData($("section select.ngBatch").val());
                        }, 1200);
                        // 打印 层卷 收线 标签
                        if (res.result) {
                            takeupPrint("gjxrollTakeup", res.result);
                        }
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        }
    });

    //点击 完工
    $(".bnt .workover").click(function () {
        var machine = $("section span.machine").html();
        if (!machine || machine == "空") {
            $alert("请先扫描层卷机台");
        }
        var ngBatch = $("section select.ngBatch").val();
        if (!ngBatch) {
            return $alert("请先选择组号！");
        }
        $.confirm("确认组号" + ngBatch + "完工吗？", function () {
            if (pass == 0) return;
            pass = 0;
            loading_show("完工中...");
            //层卷 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxroll/gjxrollComplete',
                data: {
                    batch: $("section select.ngBatch").val(),
                    machine: $("section span.machine").html()
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        pass = 1;
                        $alert("完成成功！");
                        setTimeout(function () {
                            localStorage.setItem("gjxrollMachine", $("section span.machine").html());
                            window.location.href = "gjxroll.html";
                        }, 1200);
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

});
